var $reserveForm = $("#mc-embedded-subscribe-form");

(function ($) { 
  window.fnames = new Array(); window.ftypes = new Array(); 
  var $form = $("#mc-embedded-subscribe-form");
  $form.find('input').each(
    function(index,element){
      fnames[index] = element.name;
      ftypes[index] = element.type;
    }
  )
  
  var $formButton = $reserveForm.find('#btnSubscribe');
  var $subscribeMailInput = $reserveForm.find('#subscribeMailInput');
  
  $formButton.on('click', formButtonClickHandler);
  $subscribeMailInput.on('keypress', subscribeInputKeyPressHandler);
}(jQuery));

function formButtonClickHandler(evt){
  evt.preventDefault();
  var $email = $reserveForm.find('#subscribeMailInput');
  if(checkEmail($email)){
    $reserveForm.submit();
  }
}

function subscribeInputKeyPressHandler(evt){
  if(evt.which === 13) {
    evt.preventDefault();
    var $email = $reserveForm.find('#subscribeMailInput');
    if(checkEmail($email)){
      $reserveForm.submit();
    }
  }
}

function checkEmail($email) {
  var emailReg = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
  var result = emailReg.test($email.val());
  if(!result) {
    alert("E-mail 臧掛潉 頇曥澑頃挫＜鞁嫓鞓�.");
    $email.focus();
  }
  return result;
}